﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EnvironmentalMonitor.Support.Resource;

namespace EnvironmentalMonitor.Support.Module.Environmental
{
    /// <summary>
    /// 探头
    /// </summary>
    public class Detector : EnvironmentalMonitor.Support.Module.AbstractModule
    {
        public const int VALUE_LENGTH = 2;

        private string _MachineId;
        /// <summary>
        /// 设备编号
        /// </summary>
        public string MachineId
        {
            get
            {
                return this._MachineId;
            }
            set
            {
                this._MachineId = value;
            }
        }

        private string _DetectorTypeId;
        /// <summary>
        /// 探头类型编号
        /// </summary>
        public string DetectorTypeId
        {
            get
            {
                return this._DetectorTypeId;
            }
            set
            {
                this._DetectorTypeId = value;
            }
        }

        private int _Serial;
        /// <summary>
        /// 序号
        /// </summary>
        public int Serial
        {
            get
            {
                return this._Serial;
            }
            set
            {
                this._Serial = value;
            }
        }

        private int _PositionX;
        /// <summary>
        /// X坐标位置
        /// </summary>
        public int PositionX
        {
            get
            {
                return this._PositionX;
            }
            set
            {
                this._PositionX = value;
            }
        }

        private int _PositionY;
        /// <summary>
        /// Y坐标位置
        /// </summary>
        public int PositionY
        {
            get
            {
                return this._PositionY;
            }
            set
            {
                this._PositionY = value;
            }
        }

        private Machine _Machine;
        /// <summary>
        /// 设备
        /// </summary>
        public Machine Machine
        {
            get
            {
                return this._Machine;
            }
            set
            {
                this._Machine = value;
            }
        }

        private DetectorType _DetectorType;
        /// <summary>
        /// 探头类型
        /// </summary>
        public DetectorType DetectorType
        {
            get
            {
                return this._DetectorType;
            }
            set
            {
                this._DetectorType = value;
            }
        }

        private ushort _Value;
        /// <summary>
        /// 值
        /// </summary>
        public ushort Value
        {
            get
            {
                return this._Value;
            }
            set
            {
                this._Value = value;
            }
        }

        public string RoomName
        {
            get
            {
                return this.Machine.RoomName;
            }
        }

        public string MachineName
        {
            get
            {
                return this.Machine.Name;
            }
        }

        public string DetectorTypeName
        {
            get
            {
                return this.DetectorType.Name;
            }
        }

        public byte DetectorTypeCode
        {
            get
            {
                return this.DetectorType.Code;
            }
        }

        public string DetectorTypeTypeText
        {
            get
            {
                return this.DetectorType.TypeText;
            }
        }

        public ushort DetectorTypeThreshold
        {
            get
            {
                return this.DetectorType.Threshold;
            }
        }

        public ushort DetectorTypeMinimum
        {
            get
            {
                return this.DetectorType.Minimum;
            }
        }

        public ushort DetectorTypeMaximum
        {
            get
            {
                return this.DetectorType.Maximum;
            }
        }

        public string LayoutImage
        {
            get
            {
                string result = string.Empty;
                if (this.DetectorType != null)
                {
                    if (!string.IsNullOrEmpty(this.DetectorType.Normal))
                    {
                        result = string.Format("{0}{1}", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath, this.DetectorType.Normal);
                    }
                }
                return result;
            }
        }

        public bool Error
        {
            get
            {
                bool result = true;

                if (this.DetectorType != null)
                {
                    switch (this.DetectorType.Type)
                    {
                        case DetectorTypes.Switch:
                            if ((this.Value >= this.DetectorType.Minimum) && (this.Value < this.DetectorType.Maximum))
                            {
                                result = false;
                            }
                            break;
                        case DetectorTypes.Area:
                            if ((this.Value >= this.DetectorType.Minimum) && (this.Value < this.DetectorType.Maximum))
                            {
                                result = false;
                            }
                            break;
                        case DetectorTypes.DoubleArea:
                            byte[] values = BitConverter.GetBytes(this.Value);
                            byte[] minimums = BitConverter.GetBytes(this.DetectorType.Minimum);
                            byte[] maximums = BitConverter.GetBytes(this.DetectorType.Maximum);
                            if (((values[0] >= minimums[0]) && (values[0] < maximums[0])) && ((values[1] >= minimums[1]) && (values[1] < maximums[1])))
                            {
                                result = false;
                            }
                            break;
                    }
                }

                return result;
            }
        }

        public string ViewValue
        {
            get
            {
                string result = string.Empty;

                if (this.DetectorType != null)
                {
                    switch (this.DetectorType.Type)
                    {
                        case DetectorTypes.Switch:
                            if ((this.Value >= this.DetectorType.Minimum) && (this.Value < this.DetectorType.Maximum))
                            {
                                result = this.DetectorType.DescriptionA;
                            }
                            else
                            {
                                result = this.DetectorType.DescriptionB;
                            }
                            break;
                        case DetectorTypes.Area:
                            result = string.Format("{0}{1}", this.Value, this.DetectorType.DescriptionA);
                            break;
                        case DetectorTypes.DoubleArea:
                            byte[] values = BitConverter.GetBytes(this.Value);
                            result = string.Format("{0}{1} {2}{3}", values[0], this.DetectorType.DescriptionA, values[1], this.DetectorType.DescriptionB);
                            break;
                    }
                }

                return result;
            }
        }

        public string ViewImage
        {
            get
            {
                string result = string.Empty;
                if (this.DetectorType != null)
                {
                    if (this.Error)
                    {
                        if (!string.IsNullOrEmpty(this.DetectorType.Error))
                        {
                            result = string.Format("{0}{1}", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath, this.DetectorType.Error);
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(this.DetectorType.Normal))
                        {
                            result = string.Format("{0}{1}", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath, this.DetectorType.Normal);
                        }
                    }
                }
                return result;
            }
        }

        private DateTime _RefreshTime;
        /// <summary>
        /// 刷新时间
        /// </summary>
        public DateTime RefreshTime
        {
            get
            {
                return this._RefreshTime;
            }
            set
            {
                this._RefreshTime = value;
            }
        }

        public string RefreshTimeText
        {
            get
            {
                return this._RefreshTime.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        /// <summary>
        /// 编码
        /// </summary>
        /// <returns>数据</returns>
        public byte[] Encode()
        {
            byte[] results = new byte[Detector.VALUE_LENGTH];

            switch (this.DetectorType.Type)
            {
                case Resource.DetectorTypes.Area:
                    results = BitConverter.GetBytes(this.Value);
                    Array.Reverse(results);
                    break;
                case Resource.DetectorTypes.DoubleArea:
                    results = BitConverter.GetBytes(this.Value);
                    Array.Reverse(results);
                    break;
                case Resource.DetectorTypes.Switch:
                    if (this.Value == 0)
                    {
                        results = new byte[] { 0x00, 0x00 };
                    }
                    else
                    {
                        results = new byte[] { 0x01, 0x01 };
                    }
                    break;
                default:
                    results = BitConverter.GetBytes(this.Value);
                    Array.Reverse(results);
                    break;
            }

            return results;
        }

        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="values">数据</param>
        public void Decode(byte[] values)
        {
            switch (this.DetectorType.Type)
            {
                case Resource.DetectorTypes.Area:
                    Array.Reverse(values);
                    this.Value = BitConverter.ToUInt16(values, 0);
                    break;
                case Resource.DetectorTypes.DoubleArea:
                    Array.Reverse(values);
                    this.Value = BitConverter.ToUInt16(values, 0);
                    break;
                case Resource.DetectorTypes.Switch:
                    this.Value = values[0];
                    break;
                default:
                    Array.Reverse(values);
                    this.Value = BitConverter.ToUInt16(values, 0);
                    break;
            }
        }
    }
}
